package com.bdqn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;


@SpringBootTest
public class SysUserMapperTester {

    @Resource
    private SysUserMapper sysUserMapper;

    @Test
    public void testInsert(){
        sysUserMapper.insert(new SysUser("ktjiaoyu","123456",8L,1L));
    }

    @Test
    public void testGet(){
        SysUser user = sysUserMapper.get(2L);
        System.out.println("usrName："+user.getUsrName());
    }

    @Test
    public void testDelete() {
        sysUserMapper.delete(32L);
    }

   /* @Test
    public void testFindAll() {
        List<SysUser> list = sysUserMapper.findAll();
        for (SysUser sysUser : list) {
            System.out.println(sysUser.toString());
        }
    }*/

    @Test
    public void testSelectById(){
        SysUser sysUser = sysUserMapper.selectById(2L);
        System.out.println("usrName:" + sysUser.getUsrName());
    }

    @Test
    public void testFindAll(){
        List<SysUser> list = sysUserMapper.selectList(null);
        for (SysUser sysUser : list) {
            System.out.println("usrName:" + sysUser.getUsrName());
        }
    }

    @Test
    public void testFind(){ //条件查询：根据用户名和密码查询
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("usr_name","MyBatis-Plus");
        qw.eq("usr_password","123456");
        List<SysUser> list = sysUserMapper.selectList(qw);
        if (list != null){
            for (SysUser sysUser : list) {
                System.out.println("usrId:" + sysUser.getUsrId());
            }
        }
    }

    @Test
    public void testPagination() {// 分页查询
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
        wrapper.eq("usr_role_id",2L);//查询条件：角色Id为2
        //分页对象：查询获得User对象，查看第1页数据，每页显示2条记录
        Page<SysUser> page = new Page<SysUser>(1, 2);
        IPage<SysUser> userPager = sysUserMapper.selectPage(page,wrapper);
        System.out.println("总记录数 ---- --> " + userPager.getTotal());
        System.out.println("总页数 ------ --> " + userPager.getPages());
         System.out.println("当前页数 ----- -> " + userPager.getCurrent());
         System.out.println("每页记录数 -----> " + userPager.getSize());
         System.out.println("当前记录： -----> ");
         for(SysUser user:userPager.getRecords()){//获得查询记录
         System.out.println("usrId："+user.getUsrId());
         }
    }
}
